<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button class="search-btn" slot="title" type="info" size="small" round
        icon="search" to="/search">搜索</van-button>
    </van-nav-bar>
    <!-- tab标签页 -->
    <van-tabs v-model="active" class="channel-tabs" animated swipeable>
      <van-tab v-for="channel in channels" :key="channel.id"
        :title="channel.name">
        <!-- 列表组键 -->
        <article-list :channel='channel' />
      </van-tab>
      <!-- 右侧汉堡按钮 -->
      <div slot="nav-right" class="placeholder"></div>
      <div slot='nav-right' class="hamburger-btn"
        @click="isChennelEditShow = true">
        <i class="iconfont icon-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 弹出层 -->
    <van-popup v-model='isChennelEditShow' closeable
      close-icon-position="top-left" position="bottom"
      :style="{ height: '100%' }">
      <ChannelEdit @updata_active='onUpdataActive' :active='active'
        :my-channels='channels'>
      </ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list'
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'

export default {
  // 组件名称
  name: 'homeIndex',
  // 局部注册的组件
  components: {
    ArticleList, // 列表项
    ChannelEdit // 弹出层 频道编辑
  },
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
    return {
      active: 0,
      channels: [], // id name
      // 这里我们先设置为 true 就能看到弹窗的页面了
      isChennelEditShow: false
    }
  },
  // 计算属性
  computed: {
    ...mapState(['user'])
  },
  // 侦听器
  watch: {},
  // 生命周期钩子   注：没用到的钩子请自行删除
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    this.loadChannels()
  },
  /**
  * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
  */
  mounted () { },
  // 组件方法
  methods: {
    async loadChannels () {
      try {
        const localChannels = getItem('TOUTIAO_CHANNELS')
        if (this.user || !localChannels) { // 已登录,请求用户数据
          const { data } = await getUserChannels()
          this.channels = data.data.channels
        } else {
          this.channles = localChannels
        }
        // 未登录
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },
    onUpdataActive (index, aa) { // 非编辑状态,执行跳转
      this.active = index
      this.isChennelEditShow = aa
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
}
// tab标签页
// /deep/ 深度标记符
/deep/ .channel-tabs {
  .van-tabs__wrap {
    height: 82px;
    position: fixed;
    top: 92px;
    left: 0;
    right: 0;
    z-index: 1;
    width: 100%;
  }
  .van-tab {
    min-width: 200px;
    border-right: 1px solid #edeff3;
    font-size: 30px;
    color: #777777;
  }

  .van-tab--active {
    color: #333333;
  }

  .van-tabs__nav {
    padding-bottom: 0;
  }

  .van-tabs__line {
    bottom: 8px;
    width: 31px !important;
    height: 6px;
    background: #3296fa;
  }
  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao {
      font-size: 33px;
    }
  }
  &:before {
    content: "";
    position: absolute;
    left: 0;
    width: 1px;
    height: 58px;
    background-image: url(~@/assets/gradient-gray-line.png);
    background-size: contain;
  }
}
</style>
